#include "stdafx.h"
#include "BasicMutex.h"

namespace BasicSkinTimer
{
	BasicMutex::BasicMutex(void)
	{
		m_recursionCount = 0;
		InitializeCriticalSection(&m_internalMutex);
	}

	BasicMutex::~BasicMutex(void)
	{
		DeleteCriticalSection(&m_internalMutex);
	}
	void BasicMutex::lock()
	{
		EnterCriticalSection(&m_internalMutex);
		++m_recursionCount;
	}
	bool BasicMutex::tryLock()
	{

		DWORD result = TryEnterCriticalSection(&m_internalMutex);

		if (result != 0) 
		{   
			if (m_recursionCount > 0)
			{
				LeaveCriticalSection(&m_internalMutex);
				return false;
			}

			++m_recursionCount;
			return true;
		}
		return false;
	}

	void BasicMutex::unlock()
	{
		--m_recursionCount;
		LeaveCriticalSection(&m_internalMutex);
	}
}
